<!DOCTYPE HTML>
<html lang="en" >
    <!-- Start book Flask框架 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>多对多演练 | Flask框架</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../shu-ju-ku/shu-ju-ku-qian-yi.html" />
    
    
    <link rel="prev" href="../shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/shan-chu-shu-ju.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="3.5"
        data-chapter-title="多对多演练"
        data-filepath="shu-ju-ku/duo-dui-duo-yan-lian.md"
        data-basepath=".."
        data-revision="Sat Apr 14 2018 02:27:55 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Flask框架
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="shi-tu-ji-lu-you.html">
            
                
                    <a href="../shi-tu-ji-lu-you.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        视图及路由
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="shi-tu-ji-lu-you/flaskjian-jie.html">
            
                
                    <a href="../shi-tu-ji-lu-you/flaskjian-jie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        Flask简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="shi-tu-ji-lu-you/flaskhuan-jing-an-zhuang.html">
            
                
                    <a href="../shi-tu-ji-lu-you/flaskhuan-jing-an-zhuang.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        虚拟环境
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="shi-tu-ji-lu-you/helloworld.html">
            
                
                    <a href="../shi-tu-ji-lu-you/helloworld.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        HelloWorld
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="shi-tu-ji-lu-you/xiang-guan-pei-zhi-can-shu.html">
            
                
                    <a href="../shi-tu-ji-lu-you/xiang-guan-pei-zhi-can-shu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        相关配置参数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="shi-tu-ji-lu-you/lu-you-de-ge-zhong-ding-yi.html">
            
                
                    <a href="../shi-tu-ji-lu-you/lu-you-de-ge-zhong-ding-yi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        路由基本定义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="shi-tu-ji-lu-you/shi-tu-chang-yong-luo-ji.html">
            
                
                    <a href="../shi-tu-ji-lu-you/shi-tu-chang-yong-luo-ji.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        视图常用逻辑
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="shi-tu-ji-lu-you/zheng-ze-pi-pei-lu-you.html">
            
                
                    <a href="../shi-tu-ji-lu-you/zheng-ze-pi-pei-lu-you.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        正则匹配路由
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="shi-tu-ji-lu-you/yi-chang-bu-huo.html">
            
                
                    <a href="../shi-tu-ji-lu-you/yi-chang-bu-huo.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                        异常捕获
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="shi-tu-ji-lu-you/qing-qiu-gou-zi.html">
            
                
                    <a href="../shi-tu-ji-lu-you/qing-qiu-gou-zi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.9.</b>
                        
                        请求勾子
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="shi-tu-ji-lu-you/flaskzhuang-shi-qi-lu-you-ju-ti-shi-xian.html">
            
                
                    <a href="../shi-tu-ji-lu-you/flaskzhuang-shi-qi-lu-you-ju-ti-shi-xian.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.10.</b>
                        
                        装饰器路由具体实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="shi-tu-ji-lu-you/huo-qu-qing-qiu-can-shu.html">
            
                
                    <a href="../shi-tu-ji-lu-you/huo-qu-qing-qiu-can-shu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.11.</b>
                        
                        获取请求参数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="shi-tu-ji-lu-you/zhuang-tai-bao-chi.html">
            
                
                    <a href="../shi-tu-ji-lu-you/zhuang-tai-bao-chi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.12.</b>
                        
                        状态保持
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="shi-tu-ji-lu-you/zhuang-tai-bao-chi/cookie.html">
            
                
                    <a href="../shi-tu-ji-lu-you/zhuang-tai-bao-chi/cookie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.12.1.</b>
                        
                        Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.12.2" data-path="shi-tu-ji-lu-you/zhuang-tai-bao-chi/session.html">
            
                
                    <a href="../shi-tu-ji-lu-you/zhuang-tai-bao-chi/session.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.12.2.</b>
                        
                        Session
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="shi-tu-ji-lu-you/shang-xia-wen.html">
            
                
                    <a href="../shi-tu-ji-lu-you/shang-xia-wen.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.13.</b>
                        
                        上下文
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="shi-tu-ji-lu-you/flask-script.html">
            
                
                    <a href="../shi-tu-ji-lu-you/flask-script.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.14.</b>
                        
                        Flask-Script
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="mo-ban.html">
            
                
                    <a href="../mo-ban.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="mo-ban/jinja2mo-ban-yin-qing.html">
            
                
                    <a href="../mo-ban/jinja2mo-ban-yin-qing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Jinja2简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="mo-ban/jinja2mo-ban-yin-qing-shi-yong.html">
            
                
                    <a href="../mo-ban/jinja2mo-ban-yin-qing-shi-yong.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        模板的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="mo-ban/guo-lv-qi.html">
            
                
                    <a href="../mo-ban/guo-lv-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        过滤器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="mo-ban/zi-ding-yi-guo-lv-qi.html">
            
                
                    <a href="../mo-ban/zi-ding-yi-guo-lv-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        自定义过滤器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="mo-ban/kong-zhi-dai-ma-kuai.html">
            
                
                    <a href="../mo-ban/kong-zhi-dai-ma-kuai.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        控制代码块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="mo-ban/mo-ban-dai-ma-fu-yong.html">
            
                
                    <a href="../mo-ban/mo-ban-dai-ma-fu-yong.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        模板代码复用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.6.1" data-path="mo-ban/mo-ban-dai-ma-fu-yong/hong.html">
            
                
                    <a href="../mo-ban/mo-ban-dai-ma-fu-yong/hong.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.1.</b>
                        
                        宏
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6.2" data-path="mo-ban/mo-ban-dai-ma-fu-yong/ji-cheng.html">
            
                
                    <a href="../mo-ban/mo-ban-dai-ma-fu-yong/ji-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.2.</b>
                        
                        继承
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6.3" data-path="mo-ban/mo-ban-dai-ma-fu-yong/bao-han.html">
            
                
                    <a href="../mo-ban/mo-ban-dai-ma-fu-yong/bao-han.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.3.</b>
                        
                        包含
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="mo-ban/te-you-bian-liang-he-han-shu.html">
            
                
                    <a href="../mo-ban/te-you-bian-liang-he-han-shu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        特有变量和函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="mo-ban/flask-wtfbiao-dan.html">
            
                
                    <a href="../mo-ban/flask-wtfbiao-dan.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        Flask-WTF表单
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="mo-ban/csrf.html">
            
                
                    <a href="../mo-ban/csrf.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.9.</b>
                        
                        CSRF
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="shu-ju-ku.html">
            
                
                    <a href="../shu-ju-ku.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="shu-ju-ku/orm.html">
            
                
                    <a href="../shu-ju-ku/orm.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        ORM
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="shu-ju-ku/flask-sqlalchemyan-zhuang-ji-she-zhi.html">
            
                
                    <a href="../shu-ju-ku/flask-sqlalchemyan-zhuang-ji-she-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        Flask-SQLAlchemy安装及配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="shu-ju-ku/shu-ju-ku-de-ji-ben-cao-zuo.html">
            
                
                    <a href="../shu-ju-ku/shu-ju-ku-de-ji-ben-cao-zuo.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        数据库的基本操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li.html">
            
                
                    <a href="../shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        综合案例-图书管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.4.1" data-path="shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/ding-yi-mo-xing.html">
            
                
                    <a href="../shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/ding-yi-mo-xing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.1.</b>
                        
                        定义模型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4.2" data-path="shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-ku-biao-chuang-5efa26-ce-shi-shu-ju-tian-jia.html">
            
                
                    <a href="../shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-ku-biao-chuang-5efa26-ce-shi-shu-ju-tian-jia.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.2.</b>
                        
                        数据库表创建&amp;测试数据添加
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4.3" data-path="shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-xian-793a26-biao-dan-tian-jia.html">
            
                
                    <a href="../shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-xian-793a26-biao-dan-tian-jia.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.3.</b>
                        
                        数据显示&amp;表单添加
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4.4" data-path="shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/biao-dan-yan-zheng.html">
            
                
                    <a href="../shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/biao-dan-yan-zheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.4.</b>
                        
                        表单验证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4.5" data-path="shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/shan-chu-shu-ju.html">
            
                
                    <a href="../shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/shan-chu-shu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.5.</b>
                        
                        删除数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter active" data-level="3.5" data-path="shu-ju-ku/duo-dui-duo-yan-lian.html">
            
                
                    <a href="../shu-ju-ku/duo-dui-duo-yan-lian.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        多对多演练
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="shu-ju-ku/shu-ju-ku-qian-yi.html">
            
                
                    <a href="../shu-ju-ku/shu-ju-ku-qian-yi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        数据库迁移
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="shu-ju-ku/qi-ta.html">
            
                
                    <a href="../shu-ju-ku/qi-ta.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        信号机制
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="shu-ju-ku/chang-jian-guan-xi-mo-ban-dai-ma.html">
            
                
                    <a href="../shu-ju-ku/chang-jian-guan-xi-mo-ban-dai-ma.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        常见关系模板代码
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="lan-tu-dan-yuan-ce-shi.html">
            
                
                    <a href="../lan-tu-dan-yuan-ce-shi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        蓝图&amp;单元测试
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="lan-tu-dan-yuan-ce-shi/lan-tu.html">
            
                
                    <a href="../lan-tu-dan-yuan-ce-shi/lan-tu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        蓝图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="lan-tu-dan-yuan-ce-shi/dan-yuan-ce-shi.html">
            
                
                    <a href="../lan-tu-dan-yuan-ce-shi/dan-yuan-ce-shi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        单元测试
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Flask框架</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x591A;&#x5BF9;&#x591A;&#x6F14;&#x7EC3;">&#x591A;&#x5BF9;&#x591A;&#x6F14;&#x7EC3;</h1>
<p>&#x5728;&#x9879;&#x76EE;&#x5F00;&#x53D1;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x4F1A;&#x9047;&#x5230;&#x5F88;&#x591A;&#x6570;&#x636E;&#x4E4B;&#x95F4;&#x591A;&#x5BF9;&#x591A;&#x5173;&#x7CFB;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A;</p>
<ul>
<li>&#x5B66;&#x751F;&#x7F51;&#x4E0A;&#x9009;&#x8BFE;(&#x5B66;&#x751F;&#x548C;&#x8BFE;&#x7A0B;)</li>
<li>&#x8001;&#x5E08;&#x4E0E;&#x5176;&#x6388;&#x8BFE;&#x7684;&#x73ED;&#x7EA7;(&#x8001;&#x5E08;&#x548C;&#x73ED;&#x7EA7;)</li>
<li>&#x7528;&#x6237;&#x4E0E;&#x5176;&#x6536;&#x85CF;&#x7684;&#x65B0;&#x95FB;(&#x7528;&#x6237;&#x548C;&#x65B0;&#x95FB;)</li>
<li>&#x7B49;&#x7B49;...</li>
</ul>
<p>&#x6240;&#x4EE5;&#x5728;&#x5F00;&#x53D1;&#x8FC7;&#x7A0B;&#x4E2D;&#x9700;&#x8981;&#x4F7F;&#x7528; ORM &#x6A21;&#x578B;&#x5C06;&#x8868;&#x4E0E;&#x8868;&#x7684;&#x591A;&#x5BF9;&#x591A;&#x5173;&#x8054;&#x5173;&#x7CFB;&#x4F7F;&#x7528;&#x4EE3;&#x7801;&#x63CF;&#x8FF0;&#x51FA;&#x6765;&#x3002;&#x591A;&#x5BF9;&#x591A;&#x5173;&#x7CFB;&#x63CF;&#x8FF0;&#x6709;&#x4E00;&#x4E2A;&#x552F;&#x4E00;&#x7684;&#x70B9;&#x5C31;&#x662F;&#xFF1A;<strong>&#x9700;&#x8981;&#x6DFB;&#x52A0;&#x4E00;&#x5F20;&#x5355;&#x72EC;&#x7684;&#x8868;&#x53BB;&#x8BB0;&#x5F55;&#x4E24;&#x5F20;&#x8868;&#x4E4B;&#x95F4;&#x7684;&#x5BF9;&#x5E94;&#x5173;&#x7CFB;</strong></p>
<h2 id="&#x573A;&#x666F;&#x793A;&#x4F8B;">&#x573A;&#x666F;&#x793A;&#x4F8B;</h2>
<h3 id="&#x9700;&#x6C42;&#x5206;&#x6790;">&#x9700;&#x6C42;&#x5206;&#x6790;</h3>
<ul>
<li>&#x5B66;&#x751F;&#x53EF;&#x4EE5;&#x7F51;&#x4E0A;&#x9009;&#x8BFE;&#xFF0C;&#x5B66;&#x751F;&#x6709;&#x591A;&#x4E2A;&#xFF0C;&#x8BFE;&#x7A0B;&#x4E5F;&#x6709;&#x591A;&#x4E2A;</li>
<li>&#x5B66;&#x751F;&#x6709;&#xFF1A;&#x5F20;&#x4E09;&#x3001;&#x674E;&#x56DB;&#x3001;&#x738B;&#x4E94;</li>
<li>&#x8BFE;&#x7A0B;&#x6709;&#xFF1A;&#x7269;&#x7406;&#x3001;&#x5316;&#x5B66;&#x3001;&#x751F;&#x7269;</li>
<li>&#x9009;&#x4FEE;&#x5173;&#x7CFB;&#x6709;&#xFF1A;<ul>
<li>&#x5F20;&#x4E09;&#x9009;&#x4FEE;&#x4E86;&#x5316;&#x5B66;&#x548C;&#x751F;&#x7269;</li>
<li>&#x674E;&#x56DB;&#x9009;&#x4FEE;&#x4E86;&#x5316;&#x5B66;</li>
<li>&#x738B;&#x4E94;&#x9009;&#x4FEE;&#x4E86;&#x7269;&#x7406;&#x3001;&#x5316;&#x5B66;&#x548C;&#x751F;&#x7269;</li>
</ul>
</li>
<li>&#x9700;&#x6C42;&#xFF1A;<ol>
<li>&#x67E5;&#x8BE2;&#x67D0;&#x4E2A;&#x5B66;&#x751F;&#x9009;&#x4FEE;&#x4E86;&#x54EA;&#x4E9B;&#x8BFE;&#x7A0B; </li>
<li>&#x67E5;&#x8BE2;&#x67D0;&#x4E2A;&#x8BFE;&#x7A0B;&#x90FD;&#x6709;&#x54EA;&#x4E9B;&#x5B66;&#x751F;&#x9009;&#x62E9;</li>
</ol>
</li>
</ul>
<h3 id="&#x601D;&#x8DEF;&#x5206;&#x6790;">&#x601D;&#x8DEF;&#x5206;&#x6790;</h3>
<ul>
<li>&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5206;&#x6790;&#x5F97;&#x51FA;<ul>
<li>&#x7528;&#x4E00;&#x5F20;&#x8868;&#x6765;&#x4FDD;&#x5B58;&#x6240;&#x6709;&#x7684;&#x5B66;&#x751F;&#x6570;&#x636E;</li>
<li>&#x7528;&#x4E00;&#x5F20;&#x8868;&#x6765;&#x4FDD;&#x5B58;&#x6240;&#x6709;&#x7684;&#x8BFE;&#x7A0B;&#x6570;&#x636E;</li>
</ul>
</li>
<li>&#x5177;&#x4F53;&#x8868;&#x53CA;&#x6D4B;&#x8BD5;&#x6570;&#x636E;&#x53EF;&#x4EE5;&#x5982;&#x4E0B;&#xFF1A;</li>
</ul>
<h5 id="&#x5B66;&#x751F;&#x8868;student">&#x5B66;&#x751F;&#x8868;(Student)</h5>
<table>
<thead>
<tr>
<th style="text-align:left">&#x4E3B;&#x952E;(id)</th>
<th style="text-align:left">&#x5B66;&#x751F;&#x540D;(name)</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">1</td>
<td style="text-align:left">&#x5F20;&#x4E09;</td>
</tr>
<tr>
<td style="text-align:left">2</td>
<td style="text-align:left">&#x674E;&#x56DB;</td>
</tr>
<tr>
<td style="text-align:left">3</td>
<td style="text-align:left">&#x738B;&#x4E94;</td>
</tr>
</tbody>
</table>
<h5 id="&#x9009;&#x4FEE;&#x8BFE;&#x8868;course">&#x9009;&#x4FEE;&#x8BFE;&#x8868;(Course)</h5>
<table>
<thead>
<tr>
<th style="text-align:left">&#x4E3B;&#x952E;(id)</th>
<th style="text-align:left">&#x8BFE;&#x7A0B;&#x540D;(name)</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">1</td>
<td style="text-align:left">&#x7269;&#x7406;</td>
</tr>
<tr>
<td style="text-align:left">2</td>
<td style="text-align:left">&#x5316;&#x5B66;</td>
</tr>
<tr>
<td style="text-align:left">3</td>
<td style="text-align:left">&#x751F;&#x7269;</td>
</tr>
</tbody>
</table>
<h5 id="&#x6570;&#x636E;&#x5173;&#x8054;&#x5173;&#x7CFB;&#x8868;studentcourse">&#x6570;&#x636E;&#x5173;&#x8054;&#x5173;&#x7CFB;&#x8868;(Student_Course)</h5>
<table>
<thead>
<tr>
<th style="text-align:left">&#x4E3B;&#x952E;(student.id)</th>
<th style="text-align:left">&#x4E3B;&#x952E;(course.id)</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">1</td>
<td style="text-align:left">2</td>
</tr>
<tr>
<td style="text-align:left">1</td>
<td style="text-align:left">3</td>
</tr>
<tr>
<td style="text-align:left">2</td>
<td style="text-align:left">2</td>
</tr>
<tr>
<td style="text-align:left">3</td>
<td style="text-align:left">1</td>
</tr>
<tr>
<td style="text-align:left">3</td>
<td style="text-align:left">2</td>
</tr>
<tr>
<td style="text-align:left">3</td>
<td style="text-align:left">3</td>
</tr>
</tbody>
</table>
<h3 id="&#x7ED3;&#x679C;">&#x7ED3;&#x679C;</h3>
<ul>
<li><p>&#x67E5;&#x8BE2;&#x67D0;&#x4E2A;&#x5B66;&#x751F;&#x9009;&#x4FEE;&#x4E86;&#x54EA;&#x4E9B;&#x8BFE;&#x7A0B;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;&#x67E5;&#x8BE2;&#x738B;&#x4E94;&#x9009;&#x4FEE;&#x4E86;&#x54EA;&#x4E9B;&#x8BFE;&#x7A0B;</p>
<ul>
<li>&#x53D6;&#x51FA;&#x738B;&#x4E94;&#x7684; id &#x53BB; Student_Course &#x8868;&#x4E2D;&#x67E5;&#x8BE2; student.id &#x503C;&#x4E3A; 3 &#x7684;&#x6240;&#x6709;&#x6570;&#x636E;</li>
<li>&#x67E5;&#x8BE2;&#x51FA;&#x6765;&#x6709;3&#x6761;&#x6570;&#x636E;&#xFF0C;&#x7136;&#x540E;&#x5C06;&#x8FD9;3&#x6761;&#x6570;&#x636E;&#x91CC;&#x9762;&#x7684; course.id &#x53D6;&#x503C;&#x5E76;&#x67E5;&#x8BE2; Course &#x8868;&#x5373;&#x53EF;&#x83B7;&#x5F97;&#x7ED3;&#x679C;</li>
</ul>
</li>
<li><p>&#x67E5;&#x8BE2;&#x67D0;&#x4E2A;&#x8BFE;&#x7A0B;&#x90FD;&#x6709;&#x54EA;&#x4E9B;&#x5B66;&#x751F;&#x9009;&#x62E9;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;&#x67E5;&#x8BE2;&#x751F;&#x7269;&#x8BFE;&#x7A0B;&#x90FD;&#x6709;&#x54EA;&#x4E9B;&#x5B66;&#x751F;&#x9009;&#x4FEE;</p>
<ul>
<li>&#x53D6;&#x51FA;&#x751F;&#x7269;&#x8BFE;&#x7A0B;&#x7684; id &#x53BB; Student_Course &#x8868;&#x4E2D;&#x67E5;&#x8BE2; course.id &#x503C;&#x4E3A; 3 &#x7684;&#x6240;&#x6709;&#x6570;&#x636E;</li>
<li>&#x67E5;&#x8BE2;&#x51FA;&#x6765;&#x6709;2&#x6761;&#x6570;&#x636E;&#xFF0C;&#x7136;&#x540E;&#x5C06;&#x8FD9;2&#x6761;&#x6570;&#x636E;&#x91CC;&#x9762;&#x7684; student.id &#x53D6;&#x503C;&#x5E76;&#x67E5;&#x8BE2; Student &#x8868;&#x5373;&#x53EF;&#x83B7;&#x5F97;&#x7ED3;&#x679C;</li>
</ul>
</li>
</ul>
<h2 id="&#x4EE3;&#x7801;&#x6F14;&#x7EC3;">&#x4EE3;&#x7801;&#x6F14;&#x7EC3;</h2>
<ul>
<li>&#x5B9A;&#x4E49;&#x6A21;&#x578B;&#x53CA;&#x8868;</li>
</ul>
<pre><code class="lang-python">tb_student_course = db.Table(<span class="hljs-string">&apos;tb_student_course&apos;</span>,
                             db.Column(<span class="hljs-string">&apos;student_id&apos;</span>, db.Integer, db.ForeignKey(<span class="hljs-string">&apos;students.id&apos;</span>)),
                             db.Column(<span class="hljs-string">&apos;course_id&apos;</span>, db.Integer, db.ForeignKey(<span class="hljs-string">&apos;courses.id&apos;</span>))
                             )


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span><span class="hljs-params">(db.Model)</span>:</span>
    __tablename__ = <span class="hljs-string">&quot;students&quot;</span>
    id = db.Column(db.Integer, primary_key=<span class="hljs-keyword">True</span>)
    name = db.Column(db.String(<span class="hljs-number">64</span>), unique=<span class="hljs-keyword">True</span>)

    courses = db.relationship(<span class="hljs-string">&apos;Course&apos;</span>, secondary=tb_student_course,
                              backref=<span class="hljs-string">&apos;student&apos;</span>,
                              lazy=<span class="hljs-string">&apos;dynamic&apos;</span>)


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Course</span><span class="hljs-params">(db.Model)</span>:</span>
    __tablename__ = <span class="hljs-string">&quot;courses&quot;</span>
    id = db.Column(db.Integer, primary_key=<span class="hljs-keyword">True</span>)
    name = db.Column(db.String(<span class="hljs-number">64</span>), unique=<span class="hljs-keyword">True</span>)
</code></pre>
<ul>
<li>&#x6DFB;&#x52A0;&#x6D4B;&#x8BD5;&#x6570;&#x636E;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
    db.drop_all()
    db.create_all()

    <span class="hljs-comment"># &#x6DFB;&#x52A0;&#x6D4B;&#x8BD5;&#x6570;&#x636E;</span>

    stu1 = Student(name=<span class="hljs-string">&apos;&#x5F20;&#x4E09;&apos;</span>)
    stu2 = Student(name=<span class="hljs-string">&apos;&#x674E;&#x56DB;&apos;</span>)
    stu3 = Student(name=<span class="hljs-string">&apos;&#x738B;&#x4E94;&apos;</span>)

    cou1 = Course(name=<span class="hljs-string">&apos;&#x7269;&#x7406;&apos;</span>)
    cou2 = Course(name=<span class="hljs-string">&apos;&#x5316;&#x5B66;&apos;</span>)
    cou3 = Course(name=<span class="hljs-string">&apos;&#x751F;&#x7269;&apos;</span>)

    stu1.courses = [cou2, cou3]
    stu2.courses = [cou2]
    stu3.courses = [cou1, cou2, cou3]

    db.session.add_all([stu1, stu2, stu2])
    db.session.add_all([cou1, cou2, cou3])

    db.session.commit()

    app.run(debug=<span class="hljs-keyword">True</span>)
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/shan-chu-shu-ju.html" class="navigation navigation-prev " aria-label="Previous page: 删除数据"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../shu-ju-ku/shu-ju-ku-qian-yi.html" class="navigation navigation-next " aria-label="Next page: 数据库迁移"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book Flask框架 -->
</html>
